
var applicant;
var generalInfo;
var degrees;
var isHideOn;
var hideID;


function $(id){
	return document.getElementById(id);
}

function showApp(show_id){
	//Get the root id
	var id = show_id.substr(1);
	
	// Handle the case while one hide button is on, and click another show
	if(isHideOn == true){
		var show_other_rid = "rs" + hideID;
		var hide_other_rid = "rh" + hideID;
		$(show_other_rid).style.display = "block";
		$(hide_other_rid).style.display = "none";
		generalInfo.parentNode.removeChild(generalInfo);
		degrees.parentNode.removeChild(degrees);		
	}
	isHideOn = true;
	hideID = id;
	
	// Hide this show button
	var show_rid = "rs" + id;
	var hide_rid = "rh" + id;
	
	$(show_rid).style.display = "none";
	$(hide_rid).style.display = "block";
	$("APPLICATIONINFO").style.display = "block";
	
	// Get the a_id
	var a_id = "i" + id;
	applicant = $(a_id).innerText;
	
	// Display the application
	infoQuery();	
}

function infoQuery(){
   var xmlHttp = new XMLHttpRequest();
   if (xmlHttp==null)
   {
   alert ("Your browser does not support AJAX!");
   return;
   }

   var url = "applicationinfo.jsp";
   url = url + "?applicant=" + applicant;   
   
   var responseHandler = function()
   {
      if(xmlHttp.readyState==4 ){
    	  
    	  var responseDoc = xmlHttp.responseText;

    	  var response = eval('(' + responseDoc + ')');

    	  var count = response.degrees.length;


    	  // Information about the applicant
    	  generalInfo = document.createElement("div");
		  var nameDiv = document.createElement("div");
		  var addressDiv = document.createElement("div");
		  var countryDiv = document.createElement("div");
		  var specializationDiv = document.createElement("div");
		  
		  nameDiv.innerText = "Name: " + response.degrees[0].name;
		  addressDiv.innerText = "Address: " + response.degrees[0].address;
		  countryDiv.innerText = "Country: " + response.degrees[0].country;
		  specializationDiv.innerText = "Specialization: " + response.degrees[0].specialization;
		  
		  generalInfo.appendChild(nameDiv);
		  generalInfo.appendChild(addressDiv);
		  generalInfo.appendChild(countryDiv);
		  generalInfo.appendChild(specializationDiv);
		  
		  document.getElementById("APPLICATION").appendChild(generalInfo);
		  
		  // Information of the degrees
    	  degrees = document.createElement("div");
    	  for(var i = 0; i < count; i++){

    		  var degreeDiv = document.createElement("div");
    		  degreeDiv.style.marginTop = "10px";

    		  var disciplineDiv = document.createElement("div");
    		  var universityDiv = document.createElement("div");
    		  var titleDiv = document.createElement("div");
    		  var dateDiv = document.createElement("div");
    		  var gpaDiv = document.createElement("div");
    		  var locationDiv = document.createElement("div");
    		  
    		  disciplineDiv.innerText = "Discipline: " + response.degrees[i].discipline;
    		  universityDiv.innerText = "University: " + response.degrees[i].university;
    		  titleDiv.innerText = "Title: " + response.degrees[i].title;
    		  dateDiv.innerText = "Date: " + response.degrees[i].date;
    		  gpaDiv.innerText = "GPA: " + response.degrees[i].gpa; 
    		  locationDiv.innerText = "Location: " + response.degrees[i].location;
    		  

    		  degreeDiv.appendChild(disciplineDiv);
    		  degreeDiv.appendChild(universityDiv);
    		  degreeDiv.appendChild(titleDiv);
    		  degreeDiv.appendChild(dateDiv);
    		  degreeDiv.appendChild(gpaDiv);
    		  degreeDiv.appendChild(locationDiv);
    		  
    		  degrees.appendChild(degreeDiv);

    	  }

    	  document.getElementById("APPLICATION").appendChild(degrees);
  	  
      }
   };
   xmlHttp.onreadystatechange = responseHandler;
   
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);

   return false;
}



function hideApp(hide_id){
	var id = hide_id.substr(1);
	var show_rid = "rs" + id;
	var hide_rid = "rh" + id;
	$(show_rid).style.display = "block";
	$(hide_rid).style.display = "none";
	$("APPLICATIONINFO").style.display = "none";
	
	isHideOn = false;
	
	generalInfo.parentNode.removeChild(generalInfo);
	degrees.parentNode.removeChild(degrees);
}














